/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oea.struts.actions;

import com.oea.hibernate.entities.Admin;
import com.oea.hibernate.entities.Branch;
import com.oea.hibernate.entities.Division;
import com.oea.hibernate.entities.Exam;

import com.oea.hibernate.entities.Faculty;
import com.oea.hibernate.entities.Lecture;
import com.oea.hibernate.entities.Parameter;
import com.oea.hibernate.entities.Question;
import com.oea.hibernate.entities.Result;
import com.oea.hibernate.entities.Student;
import com.oea.hibernate.entities.Subject;
import com.oea.hibernate.entities.Test;
import com.oea.hibernate.entities.Topic;
import com.oea.struts.forms.UserLoginForm;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author unnati
 */
public class UserLoginAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static String path;

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

       // request.getSession(true).invalidate();
        
        // GETTING ALL THE TOPICS AND PUTTING IN APPLICATION SCOPE
        List<Topic> alltopics = (List<Topic>) request.getSession().getAttribute("alltopics");
        if (alltopics == null) {
            alltopics = (List<Topic>) new Topic().getAll();
        }
        request.getSession().setAttribute("li_alltopics", alltopics);

        // GETTING ALL THE BRANCHES AND PUTTING IN APPLICATION SCOPE
        List<Branch> allbranches = (List<Branch>) request.getServletContext().getAttribute("allbranches");
        if (allbranches == null) {
            allbranches = (List<Branch>) new Branch().getAll();
        }
        request.getServletContext().setAttribute("li_allbranches", allbranches);

        // GETTING ALL THE Students AND PUTTING IN APPLICATION SCOPE
//        List<Student> allstudents = (List<Student>) request.getSession().getAttribute("li_allstudents");
//        if (allstudents == null) {
//            allstudents = (List<Student>) new Student().getStudentByStatus(true);
//        }
//        request.getSession().setAttribute("li_allstudents", allstudents);
        // getting all the lectures 
        List<Lecture> all_lectures = null;
        all_lectures = (List<Lecture>) request.getServletContext().getAttribute("alllectures");
        if (all_lectures == null) {
            all_lectures = (List<Lecture>) new Lecture().getAll();
            request.getServletContext().setAttribute("alllectures", all_lectures);
        }

        // GETTING ALL THE SUBJECTS AND PUTTING IN APPLICATION SCOPE
        List<Subject> allsubjects = (List<Subject>) request.getServletContext().getAttribute("li_allsubjects");
        if (allsubjects == null) {
            allsubjects = (List<Subject>) new Subject().getAll();
        }
        
        request.getServletContext().setAttribute("li_allsubjects", allsubjects);

        // GETTING ALL THE DIVISIONS AND PUTTING IN APPLICATION SCOPE
         
        List<Division> alldivisions = (List<Division>) request.getServletContext().getAttribute("li_alldivisions");
        if (alldivisions == null) {
            alldivisions = (List<Division>) new Division().getAll();
        }
        
        request.getServletContext().setAttribute("li_alldivisions", alldivisions);


        
       

        // GETTING ALL THE FACULTIES AND PUTTING IN APPLICATION SCOPE
        List<Faculty> allfaculties = (List<Faculty>) request.getServletContext().getAttribute("li_allfaculties");
        if (allfaculties == null) {
            allfaculties = (List<Faculty>) new Faculty().getAll();
        }
        for (Faculty faculty : allfaculties) {
            faculty.setAdmin();
        }
        request.getServletContext().setAttribute("li_allfaculties", allfaculties);

        // GETTING ALL THE EXAMS AND PUTTING IN Session SCOPE
        List<Exam> allexams = (List<Exam>) request.getSession().getAttribute("li_allexams");
        if (allexams == null) {
            allexams = (List<Exam>) new Exam().getAll();
        }
        
        request.getSession().setAttribute("li_allexams", allexams);

        // GETTING ALL THE TESTS AND PUTTING IN Session SCOPE
        List<Test> alltests = (List<Test>) request.getSession().getAttribute("li_alltests");
        if (alltests == null) {
            alltests = (List<Test>) new Test().getAll();
        }
        request.getSession().setAttribute("li_alltests", alltests);

        //GETTING ALL THE parameters AND PUTTING IN Session SCOPE
        Parameter allparameters = (Parameter) request.getSession().getAttribute("parameters");
         List<Parameter> li=null;
        if (allparameters == null) {
            li = (List<Parameter>) new Parameter().getAll();
        }
        if (li.size() > 0) {
            request.getSession().setAttribute("parameters", li.get(0));
        }
        
        System.out.println(request.getSession().getAttribute("parameters"));
        
        UserLoginForm ulf = (UserLoginForm) form;
        
        if ("student".equals(ulf.getUsertype())) {
            Student student = new Student();
            
            student.setEmail(ulf.getEmail());
            student.setPassword(ulf.getPassword());
            
            student = student.verify();
            if (student != null) {
                path = "user_home";
                // get all my exams and put in sessionscope
                List<Exam> myexams = student.getMyExams();
                
                request.getSession(true).setAttribute("student", student);
                request.getSession().setAttribute("usertype", ulf.getUsertype());
                request.getSession().setAttribute("myexams", myexams);
            } else {
                path = "user_login";
            }
            
        }
        
        if ("faculty".equals(ulf.getUsertype())) {
            Faculty f = new Faculty();
            
            f.setEmail(ulf.getEmail());
            f.setPassword(ulf.getPassword());
            
            f = f.verify();
            if (f != null) {

                //GETTING ALL THE RESULTS AND PUUTING INTO SESSION SCOPE    
                List<Result> allresults = (List<Result>) request.getSession().getAttribute("li_allresults");
                if (allresults == null) {
                    allresults = (List<Result>) new Result().getAll();
                }
                request.getSession().setAttribute("li_allresults", allresults);

                //GETTING ALL THE QUESTIONS AND PUUTING INTO SESSION SCOPE    
                List<Question> allquestions = (List<Question>) request.getSession().getAttribute("li_allquestions");
                if (allquestions == null) {
                    allquestions = (List<Question>) new Question().getAll();
                }
                request.getSession().setAttribute("li_allquestions", allquestions);
                
                path = "user_home";
                request.getSession().setAttribute("faculty", f);
                request.getSession().setAttribute("usertype", ulf.getUsertype());
            } else {
                path = "user_login";
            }
        }
        
        if ("admin".equals(ulf.getUsertype())) {
            Admin ad = new Admin();
            
            ad.setEmail(ulf.getEmail());
            ad.setPassword(ulf.getPassword());
            
            ad = ad.verify();
            if (ad != null) {

//                List<Result> allresults = (List<Result>) request.getSession().getAttribute("li_allresults");
//                if (allresults == null) {
//                    allresults = (List<Result>) new Result().getAll();
//                }
//                System.out.println(allresults.size());
//                request.getSession().setAttribute("li_allresults", allresults);
//                Student s = new Student();
//                List<Object> li_approve_student = (List<Object>) s.getStudentByStatus(false);
//                double iteration_approve_student = Math.ceil((float) li_approve_student.size() / 5);
//                request.getSession().setAttribute("iteration_approve_student", iteration_approve_student);
//                ArrayList<Object> filter_approve_student = Student.page(li_approve_student);
//
//                request.getServletContext().setAttribute("approve_student_viewlist", filter_approve_student);
                path = "user_home";
                System.out.println("Home = " + path);
                request.getSession().setAttribute("admin", ad);
                request.getSession().setAttribute("usertype", ulf.getUsertype());
            } else {
                path = "user_login";
            }
        }
        
        return mapping.findForward(path);
    }
}
